import QtQuick

Item {
    property var target
    property int scaleSize
    property int baseHeight
    property int baseHeight2
    property int j

    width: 0
    height: 0
    z:1

    //右下
    Rectangle{
        id:rightAndBottom
        z:1
        width: 20
        height: editMode?width:0
        color:"red"
        // anchors.bottom: target.bottom
        // anchors.right: target.right
        x:target.width-20
        y:target.height-20

        MouseArea{
            preventStealing: true
            cursorShape: Qt.SizeFDiagCursor
            anchors.fill: parent

            onPressed: {
                target.resizing = true;
                target.startPos = Qt.point(mouseX, mouseY);
            }

            onPositionChanged: {
                if(!resizing){
                    return
                }
                var toAddWidth = mouseX - target.startPos.x;
                var toAddHeight = mouseY - target.startPos.y;
                var newWidth = target.width + toAddWidth;
                var newHeight = target.height + toAddHeight;
                var minWidth=target.minWandH.x;
                // var theTargetChildRect=getTheChildRect(0,j)

                if(newWidth>=minWidth){
                    target.kwidth = newWidth;
                    // theTargetChildRect.width=
                }
                target.kheight = newHeight;
            }

            onReleased: {
                var newHeightInt=Math.round(target.kheight/baseHeight2)
                var minHeightInt=target.minWandH.y
                // console.log(target.y/baseHeight)
                target.baseHeightInt=Math.max(minHeightInt,newHeightInt)
                target.kheight=target.baseHeightInt*baseHeight
                target.resizing = false;
            }
        }


    }
    //右上
    Rectangle{
        x:target.width-20
        z:1
        width: 20
        height: editMode?width:0
        color:"red"
        // anchors.top: target.top
        // anchors.right: target.right
        MouseArea{
            preventStealing: true
            cursorShape: Qt.SizeBDiagCursor
            anchors.fill: parent
            onPressed: {
                target.resizing = true;
                target.startPos = Qt.point(mouseX, mouseY);
            }

            onPositionChanged: {
                if(!resizing){
                    return
                }
                var toAddWidth = mouseX - target.startPos.x;
                var toAddHeight = mouseY - target.startPos.y;
                var newWidth = target.width + toAddWidth;
                var newY=target.y+toAddHeight
                var newHeight = target.height -toAddHeight;
                var minWidth=target.minWandH.x;
                if(newWidth>=minWidth){
                    target.kwidth = newWidth;
                }
                target.realY= newY;
                target.kheight=newHeight
            }

            onReleased: {
                var newY=(Math.round((target.realY)/baseHeight2))*baseHeight2
                console.log(newY)

                // console.log(target.y/baseHeight)
                var minY=kchildRect_listmodel.count-1
                // console.log(minY)
                target.realY=Math.min(newY,minY*baseHeight2)
                if(newY>=minY*baseHeight2) return;
                var newHeightInt=Math.round((target.kheight)/baseHeight2)
                var minHeightInt=target.minWandH.y
                target.baseHeightInt=Math.max(minHeightInt,newHeightInt)
                target.kheight=target.baseHeightInt*baseHeight
                target.resizing = false;
            }

        }


    }
    //左上
    Rectangle{
        z:1
        width: 20
        height: editMode?width:0
        color:"red"
        // anchors.top: target.top
        // anchors.left: target.left
        MouseArea{
            cursorShape: Qt.SizeFDiagCursor
            anchors.fill: parent
            preventStealing: true
            onPressed: {
                target.resizing = true;
                target.startPos = Qt.point(mouseX, mouseY);
            }

            onPositionChanged: {
                if(!resizing){
                    return
                }
                var toAddWidth = mouseX - target.startPos.x;
                var toAddHeight = mouseY - target.startPos.y;
                var newWidth = target.width - toAddWidth;
                var newX=target.x+toAddWidth

                var newHeight = target.height -toAddHeight;
                var newY=target.y+toAddHeight

                target.xStart=newX
                var minWidth=target.minWandH.x;
                if(newWidth>=minWidth){
                    target.kwidth = newWidth;
                }
                target.realY= newY;
                target.kheight=newHeight
            }

            onReleased: {
                var newY=(Math.round((target.realY)/baseHeight2))*baseHeight2
                console.log(newY)

                // console.log(target.y/baseHeight)
                var minY=kchildRect_listmodel.count-1
                // console.log(minY)

                target.realY=Math.min(newY,minY*baseHeight2)
                if(newY>=minY*baseHeight2) return;
                var newHeightInt=Math.round((target.kheight)/baseHeight2)
                var minHeightInt=target.minWandH.y
                target.baseHeightInt=Math.max(minHeightInt,newHeightInt)
                target.kheight=target.baseHeightInt*baseHeight
                target.resizing = false;
            }



        }


    }

    // 左下
    Rectangle{
        y:target.height-20
        z:1
        width: 20
        height: editMode?width:0
        color:"red"
        // anchors.bottom: target.bottom
        // anchors.left : target.left
        MouseArea{
            preventStealing: true
            cursorShape: Qt.SizeBDiagCursor
            // cursorShape: Qt.SizeFDiagCursor
            anchors.fill: parent
            onPressed: {
                target.resizing = true;
                target.startPos = Qt.point(mouseX, mouseY);
            }

            onPositionChanged: {
                if(!resizing){
                    return
                }
                var toAddWidth = mouseX - target.startPos.x;
                var toAddHeight = mouseY - target.startPos.y;
                var newWidth = target.width - toAddWidth;
                var newX=target.x+toAddWidth

                var newHeight = target.height +toAddHeight;
                // var newY=target.y+toAddHeight

                target.xStart=newX
                var minWidth=target.minWandH.x;
                if(newWidth>=minWidth){
                    target.kwidth = newWidth;
                }
                // target.realY= newY;
                target.kheight=newHeight
            }

            onReleased: {
                // var newY=(Math.round((target.realY)/baseHeight2))*baseHeight2
                // console.log(newY)

                // // console.log(target.y/baseHeight)
                // var minY=kchildRect_listmodel.count-1
                // // console.log(minY)
                // target.realY=Math.min(newY,minY*baseHeight2)
                // if(newY>=minY*baseHeight2) return;
                var newHeightInt=Math.round((target.kheight)/baseHeight2)
                var minHeightInt=target.minWandH.y
                target.baseHeightInt=Math.max(minHeightInt,newHeightInt)
                target.kheight=target.baseHeightInt*baseHeight
                target.resizing = false;
            }
        }


    }


}
